package org.imperativetable.render;

import java.io.PrintWriter;
import java.io.Writer;
import java.util.List;
import org.imperativetable.Table;

public class HtmlTableRenderer implements TableRenderer {

    public void render(Writer writer, Table table) {
        PrintWriter out = new PrintWriter(writer);
        Object[] heades = table.getHeaders();
        List<Object[]> rows = table.getCurrentTablePage();

        //Start render table
        out.println("<table>");

        //Start render headers
        out.println("<tr>");
        for (Object header : heades) {
            out.println("<th>");
            out.println(header.toString());
            out.println("</th>");
        }
        out.println("</tr>");
        //end render headers
        
        //Start render table body
        for (Object[] row : rows) {
            out.println("<tr>");
            for (Object cell : row) {
                out.println("<td>");
                out.println(cell.toString());
                out.println("</td>");
            }
            out.println("</tr>");
        }
        //End render table body

        out.println("</table>");
        //End render table

        out.flush();
    }
}
